package invoker;

import command.Command;
import command.concreteCommand.NoCommand;

import java.util.Stack;

// 调用者 - 遥控器
public class RemoteControl {
    private Command[] onCommands;
    private Command[] offCommands;
    private Command undoCommand;
    private Stack<Command> history = new Stack<>();
    
    public RemoteControl(int slots) {
        onCommands = new Command[slots];
        offCommands = new Command[slots];
        
        // 初始化空命令
        Command noCommand = new NoCommand();
        for (int i = 0; i < slots; i++) {
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
        undoCommand = noCommand;
    }
    
    public void setCommand(int slot, Command onCommand, Command offCommand) {
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }
    
    public void pressOnButton(int slot) {
        onCommands[slot].execute();
        history.push(onCommands[slot]);
    }
    
    public void pressOffButton(int slot) {
        offCommands[slot].execute();
        history.push(offCommands[slot]);
    }
    
    public void pressUndoButton() {
        if (!history.isEmpty()) {
            Command lastCommand = history.pop();
            lastCommand.undo();
        }
    }
}